//
//  StatusScene.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-24.
//
//

#include "StatusScene.h"
#include "DictionaryKeyNames.h"
#include "NavBar.h"
#include "DataCenter.h"

StatusScene *StatusScene::createScene()
{
    StatusScene *scene = new StatusScene();
    if (scene && scene -> init()) {
        scene -> autorelease();
    }
    else {
        CC_SAFE_RELEASE(scene);
    }
    return scene;
}

bool StatusScene::init()
{
    if (!Scene::init()) {
        return false;
    }
    m_layer = Layer::create();
    this -> addChild(m_layer);
    
    auto  bg = Sprite::create("background_blur.png");
    bg -> setPosition(VISIBLESIZE * 0.5f);
    m_layer -> addChild(bg);
    
    this -> createNavi();
    
    Size containerSize = Size(450, 150);
    _container = ui::Scale9Sprite::create("m_container_bg@2x.png", Rect(0, 0, 27, 26), Rect(10, 10, 1, 1));
    _container -> setAnchorPoint(Point(0.5f, 0.5f));
    _container -> setPosition(Point(VISIBLESIZE.width * 0.5, VISIBLESIZE.height * 0.2));
    _container -> setPreferredSize(containerSize);
    
    m_layer -> addChild(_container, 10);
    
    
    ui::Button *deleteBtn = ui::Button::create("delete.png");
    deleteBtn -> setPosition(Point(containerSize.width * 0.80f, containerSize.height * 0.75f));
    deleteBtn -> addTouchEventListener(CC_CALLBACK_2(StatusScene::delelteBtnPressed, this));
    _container -> addChild(deleteBtn);
    
    auto icon = Sprite::create("plant_type_1_w@2x.png");
    icon -> setAnchorPoint(Point(0, 0.5f));
    icon -> setPosition(Point(containerSize.width * 0.15f, containerSize.height * 0.75f));
    _container -> addChild(icon);
    
    auto nameLbl = Label::createWithSystemFont("菊花", "微软雅黑", 30);
    nameLbl -> setAnchorPoint(Point(0, 0.5));
    nameLbl -> enableOutline(Color4B(255, 255, 255, 255));
    nameLbl -> setPosition(Point(icon -> getPositionX() + icon -> getContentSize().width + 10, icon -> getPositionY()));
    _container -> addChild(nameLbl);
    
    auto lvSpr = Sprite::create("i_lv_1@2x.png");
    lvSpr -> setAnchorPoint(Point(0, 0.5f));
    lvSpr -> setPosition(Point(icon -> getPositionX(), containerSize.height * 0.2f));
    _container -> addChild(lvSpr);
    
    auto expbg = ui::Scale9Sprite::create("exp_bar_bg@2x.png", Rect(0, 0, 20, 18), Rect(10, 10,1,1));
    expbg -> setPreferredSize(Size(250, 18));
    expbg -> setAnchorPoint(Point(0, 0.5f));
    expbg -> setPosition(Point(lvSpr -> getPositionX() + lvSpr -> getContentSize().width + 10, lvSpr -> getPositionY()));
    _container -> addChild(expbg);
    
    auto exp = ui::Scale9Sprite::create("exp_bar@2x.png", Rect(0, 0, 20, 18), Rect(10, 10,1,1));
    exp -> setAnchorPoint(Point(0, 0.5f));
    exp -> setPosition(expbg -> getPosition());
    exp -> setPreferredSize(Size(250 * mexp / lvExp, 18));
    _container -> addChild(exp);

    
    m_progress = XKProgressTimer::createWithBG("ring_progress@2x.png", "ring_bg@2x.png");
    m_progress -> setPosition(Point(VISIBLESIZE.width * 0.5f, _container -> getPositionY() + containerSize.height * 0.8 + m_progress -> getContentSize().height * 0.5f));
    m_progress -> setPercentage(0);
    m_layer -> addChild(m_progress, 10);
    
    Size size = m_progress -> getContentSize();
    
    auto todayLbl = Label::createWithSystemFont("今日", "微软雅黑", 40);
    todayLbl -> setTextColor(Color4B(102, 102, 102, 255));
    todayLbl -> setPosition(Point(size.width * 0.5f, size.height * 0.72f));
    m_progress -> addChild(todayLbl);
    
    auto mlLbl = Label::createWithSystemFont("ml", "微软雅黑", 35);
    mlLbl -> setTextColor(Color4B(102, 102, 102, 255));
    mlLbl -> setPosition(Point(size.width * 0.85f, size.height * 0.45f));
    m_progress -> addChild(mlLbl);
    
    haveDrinkLbl = Label::createWithSystemFont("", "微软雅黑", 100);
    haveDrinkLbl -> setTextColor(Color4B(18,98,153,255));
    haveDrinkLbl -> enableOutline(Color4B(18,98,153,255), 5);
    haveDrinkLbl->setPosition(size * 0.5f);
    m_progress -> addChild(haveDrinkLbl);
    
    
    auto infoLbl = Label::createWithSystemFont("of                ml", "Arial", 20);
    infoLbl -> setPosition(Point(size.width * 0.5f, size.height * 0.25f));
    infoLbl -> setTextColor(Color4B(102, 102, 102, 255));
    m_progress -> addChild(infoLbl);
    
    auto highlightbg = ui::Scale9Sprite::create("m_container_bg@2x.png", Rect(0, 0, 27, 26), Rect(10, 10, 1, 1));
    highlightbg -> setPosition(infoLbl -> getPosition());
    highlightbg -> setPreferredSize(Size(62, 26));
    m_progress -> addChild(highlightbg, 10);
    
    toDrinkLbl = Label::createWithSystemFont("", "Arial", 20);
    toDrinkLbl -> setPosition(highlightbg -> getPosition());
    toDrinkLbl -> setTextColor(Color4B(0,0,0,200));
    m_progress -> addChild(toDrinkLbl);
    
    
    return true;
}

void StatusScene::delelteBtnPressed(Ref *pSender, ui::Button::TouchEventType type)
{
    if (type == ui::Button::TouchEventType::ENDED) {
        PlayNegativeSound
        DataCenter::getInstance() -> clear();
        this -> initData();
        
        CallFunc *call = CallFunc::create([](){
            Scene *preScene = Director::getInstance() -> getPreviousScene();
            TransitionScene *transition = TransitionFade::create(0.3f, preScene, Color3B(255, 255, 255));
            Director::getInstance() -> popSceneWithTransition(transition);
        });
        this -> runAction(Sequence::create(DelayTime::create(kFreshTime + 0.2f),call, NULL));
    }
}

void StatusScene::createNavi()
{
    NavBar *navBar = NavBar::create();
    navBar -> setAnchorPoint(Point(0.5, 1));
    navBar -> setPosition(Point(VISIBLESIZE.width * 0.5f, VISIBLESIZE.height));
    m_layer -> addChild(navBar, 10);
}

void StatusScene::update(float delta)
{
    updateLbl((delta / kFreshTime) * tmpForAnimation);
}

void StatusScene::updateLbl(float num)
{
    
    float cur = Value(haveDrinkLbl -> getString()).asFloat();
    cur += num;
    if ((num > 0 && cur >= haveDrinked) || (cur <= haveDrinked && num < 0)) {
        this -> unscheduleUpdate();
        haveDrinkLbl -> setString(Value(int(haveDrinked)).asString());
        return;
    }
//    if (num <= haveDrinked && num < 0) {
//        this -> unscheduleUpdate();
//        haveDrinkLbl -> setString(Value(int(haveDrinked)).asString());
//        return;
//    }
    haveDrinkLbl -> setString(Value(int(cur)).asString());
}

void StatusScene::initData()
{
    tmpForAnimation = haveDrinked;
    haveDrinked = DataCenter::getInstance() -> getCapacityHaveDrink();
    tmpForAnimation = haveDrinked - tmpForAnimation;
    toDrink = DataCenter::getInstance() -> getCapacityToDrink();
    mexp = 50;
    lvExp = 100;
    
    toDrinkLbl -> setString(Value((int)toDrink).asString());
    
    m_progress -> runAction(ProgressTo::create(kFreshTime, haveDrinked / toDrink * 100 < 100 ? haveDrinked / toDrink * 100 : 100));
    this -> scheduleUpdate();
}

void StatusScene::onEnter()
{
    Scene::onEnter();
    
    this -> initData();
}

void StatusScene::onExit()
{
    Scene::onExit();
}